package com.lenovo.dil.tiltbaseddemo.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class PreferenceUtil {

	public static final String tilt_gain = "tilt_gain",
			move_up_init_angle = "move_up_init_angle",
			move_down_init_angle = "move_down_init_angle",
			over_icon_speed_ratio = "over_icon_speed_ratio",
			select_app_time_delay = "select_app_time_delay",
			move_up_down_time_delay = "move_up_down_time_delay";

	private Context mContext;
	private static PreferenceUtil instance;
	private SharedPreferences prfs;

	public synchronized static void init(Context context) {
		if (instance == null) {
			instance = new PreferenceUtil(context);
		}
	}

	public static PreferenceUtil getInstance() {
		return instance;
	}

	private PreferenceUtil(Context context) {
		mContext = context;
		prfs = PreferenceManager.getDefaultSharedPreferences(mContext);
	}

	public void updatePreference(String key, String value) {
		Editor editor = prfs.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public void updatePreference(String key, boolean value) {
		Editor editor = prfs.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public void updatePreference(String key, int value) {
		Editor editor = prfs.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public void updatePreference(String key, float value) {
		Editor editor = prfs.edit();
		editor.putFloat(key, value);
		editor.commit();
	}

	public String getStringPreference(String key) {
		return prfs.getString(key, null);
	}

	public String getStringPreference(String key, String defValue) {
		return prfs.getString(key, defValue);
	}

	public boolean getBooleanPreference(String key) {
		return prfs.getBoolean(key, true);
	}

	public boolean getBooleanPreference(String key, boolean defValue) {
		return prfs.getBoolean(key, defValue);
	}

	public int getIntPreference(String key) {
		return prfs.getInt(key, 0);
	}

	public int getIntPreference(String key, int defValue) {
		return prfs.getInt(key, defValue);
	}

	public float getFloatPreference(String key) {
		return prfs.getFloat(key, 0f);
	}

	public float getIntPreference(String key, float defValue) {
		return prfs.getFloat(key, defValue);
	}

	public static int get_tilt_gain() {
		return PreferenceUtil.getInstance().getIntPreference(tilt_gain, 20);
	}
	public static void set_tilt_gain(int param_tilt_gain) {
		PreferenceUtil.getInstance().updatePreference(tilt_gain, param_tilt_gain);
	}
	public static int get_move_up_init_angle() {
		return PreferenceUtil.getInstance().getIntPreference(move_up_init_angle, 50);
	}
	public static void set_move_up_init_angle(int param_move_up_init_angle) {
		PreferenceUtil.getInstance().updatePreference(move_up_init_angle, param_move_up_init_angle);
	}
	public static int get_move_down_init_angle() {
		return PreferenceUtil.getInstance().getIntPreference(move_down_init_angle, 10);
	}
	public static void set_move_down_init_angle(int param_move_down_init_angle) {
		PreferenceUtil.getInstance().updatePreference(move_down_init_angle, param_move_down_init_angle);
	}
	public static float get_over_icon_speed_ratio() {
		return PreferenceUtil.getInstance().getIntPreference(over_icon_speed_ratio, 0.5f);
	}
	public static void set_over_icon_speed_ratio(float param_over_icon_speed_ratio) {
		PreferenceUtil.getInstance().updatePreference(over_icon_speed_ratio, param_over_icon_speed_ratio);
	}
	public static int get_select_app_time_delay() {
		return PreferenceUtil.getInstance().getIntPreference(select_app_time_delay, 1000);
	}
	public static void set_select_app_time_delay(int param_select_app_time_delay) {
		PreferenceUtil.getInstance().updatePreference(select_app_time_delay, param_select_app_time_delay);
	}
	public static int get_move_up_down_time_delay() {
		return PreferenceUtil.getInstance().getIntPreference(move_up_down_time_delay, 500);
	}

	public static void set_move_up_down_time_delay(
			int param_move_up_down_time_delay) {
		PreferenceUtil.getInstance().updatePreference(move_up_down_time_delay, param_move_up_down_time_delay);
	}
}
